﻿using System;
using System.Windows.Forms;
using ExceptionReporting;
using Microsoft.Reporting.WinForms;
using SmartHealthCare.Properties;
using SmartHealthCare.Utils;

namespace SmartHealthCare.Frm
{
    public partial class FrmBrowser : Form
    {
        public FrmBrowser(string report)
        {
            InitializeComponent();
            AddReport(report);
        }

        public void AddReport(string reportName)
        {
            try
            {
                var viewer = new ReportViewer { ShowCredentialPrompts = true };
                viewer.BookmarkNavigation += (ViewerBookmarkNavigation);
                //
                viewer.ServerReport.ReportPath = reportName;
                viewer.ServerReport.ReportServerCredentials.NetworkCredentials = new System.Net.NetworkCredential("Administrator", "");
                                                                 

                viewer.ServerReport.ReportServerUrl = new Uri(Settings.Default.ReportBrowser, UriKind.Absolute);
                viewer.ProcessingMode = ProcessingMode.Remote;
                viewer.Dock = DockStyle.Fill;
                viewer.ServerReport.Refresh();
                //
                var title = viewer.ServerReport.ReportPath.Substring(viewer.ServerReport.ReportPath
                                                                         .LastIndexOf("/"))
                    .Replace("_", " ")
                    .Replace("/", "").Trim().ToProperCase();

                var myTabPage = new TabPage(title);
                myTabPage.Controls.Add(viewer);
                //
                myTabPage.ContextMenuStrip = _contextMenuStrip;
                //
                _tabControl.TabPages.Add(myTabPage);
                _tabControl.SelectTab(myTabPage);
                viewer.RefreshReport();
            }
            catch (Exception ex)
            {
                var reporter = new ExceptionReporter();
                reporter.ReadConfig();
                reporter.Show(ex);
            }
        }

        private static void ViewerBookmarkNavigation(object sender, BookmarkNavigationEventArgs e)
        {

        }

        private void FrmBrowser_Load(object sender, EventArgs e)
        {

            FillActiveChildFormToClient();
        }


        private void FillActiveChildFormToClient()
        {
            Bounds = Parent.ClientRectangle;
            MaximumSize = Size;
        }

        private void CerrarToolStripMenuItemClick(object sender, EventArgs e)
        {
            var current = _tabControl.SelectedTab;
            if (current != null)
                _tabControl.TabPages.Remove(current);
            //
            if (_tabControl.TabCount <= 0)
                Close();
        }
    }

}

